<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2020/5/21
 * Time: 16:15
 */

namespace app\adminapiv3\service;


class LocalInfo extends Base
{
    /**
     * @name 磁盘占用
     * @return array
     * @author wx
     * @date 2020/7/14 17:01
     */
    function disk()
    {
        $systemType = php_uname('s');

        $systemType = strtolower($systemType);

        $res = strstr($systemType,'win');

        $return = array();

        if($res){//windows
            exec('wmic logicaldisk get FreeSpace,size /format:list', $out, $status);

            $hd = '';
            foreach ($out as $vaule) {
                $hd .= $vaule . ' ';;
            }
            $hd_array = explode('   ', trim($hd));

            $key = 'CDEFGHIJKLMNOPQRSTUVWXYZ';

            foreach ($hd_array as $k => $v) {
                $s_array = explode('Size=', $v);
                $fs_array = explode('FreeSpace=', $s_array[0]);

                $size = round(trim($s_array[1]) / (1024 * 1024 * 1024), 1);
                $freespace = round(trim($fs_array[1]) / (1024 * 1024 * 1024), 1);

                $used = $size - $freespace;

                $tmp['filesystem'] = $key[$k] .'盘';//盘符
                $tmp['blocks'] = $size.'G';//总大小
                $tmp['used'] = $used .'G';//已用
                $tmp['available'] = $freespace .'G';//可用
                $tmp['use%'] = $size == 0 ? '0%' : round(($used / $size) * 100) .'%';//可用
                $tmp['mounted_on'] = $key[$k] .'盘';//挂载点

                $return[] = $tmp;
            }

        }else{//linux
            exec('df', $output, $status);

            array_shift($output);

            foreach ($output as $v)
            {
                preg_match('/(.*?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\s+(\/.*?)$/i', $v, $out);

                $return[] = [
                    'filesystem' => $out[1],//真实路径
                    'blocks' => round($out[2] / (1024 * 1024),1). 'G',
                    'used' => round($out[3] /(1024 * 1024),1) .'G',
                    'available' => round($out[4] / (1024 * 1024),1). 'G',
                    'use%' => $out[5],
                    'mounted_on'=> $out[6],
                ];
            }
        }

        return $return;
    }
}